﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using Heyo;
using Heyo.Class.Helper.Network;
using Heyo.Launcher.Models;
using Heyo.Launcher.Models.Data;
using HeyoUi.Controllers;
using HeyoUi.DialogPages;
using HeyoUi.Models;
using HeyoUi.Models.Data.Game;
using HeyoUi.Views;
using Microsoft.Win32;

namespace HeyoUi.ViewModel
{
    internal class MainWindowViewModel:NotifyPropertyChange
    {

        public MainWindowViewModel()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            CloseCommand =new DelegateCommand(CloseLauncherAction);
            SelectBgCommand=new DelegateCommand(SelectBg);
            OpenAddWindowCommand=new DelegateCommand(OpenAddWindow);
            if (!(SettingModel.Setting.BackgroundImageUrl?.Length > 0))
            {
                SettingModel.Setting.BackgroundImageUrl = DefaultBgUrl;
            }
        }


        public SettingJson Setting { get; set; } = SettingModel.Setting;
        private static string DefaultBgUrl=> $"pack://application:,,,/Resources/bg_{(DateTime.Now.Hour >= 5 && DateTime.Now.Hour <= 15 ? 1 : DateTime.Now.Hour < 18 ? 2 : 3)}.jpg";
        private static void OpenAddWindow(object obj)
        {
            new DialogWindow(new AddGameDialogPage()).Show();
        }

        private void SelectBg(object obj)
        {

            OpenFileDialog openFileDialog = new OpenFileDialog()
            {
                Filter = "图片文件|*.png;*.jpg;*bmp;*.jpeg"
            };
            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                BackgroundImageUrl = openFileDialog.FileName;
            }
        }

        public string BackgroundImageUrl
        {
            get => SettingModel.Setting.BackgroundImageUrl;
            set
            {
                if (value == SettingModel.Setting.BackgroundImageUrl) return;
                if(value.Length==0)
                    value= DefaultBgUrl;
                SettingModel.Setting.BackgroundImageUrl = value;
                //OnPropertyChanged();
            }
        }

        public GameListModel GameListModel { get; } = GameListModel.Instance;
        //public LauncherModel LauncherModel { get; } = LauncherModel.Instance;

        public DelegateCommand OpenAddWindowCommand { get; }
        public DelegateCommand CloseCommand { get; }
        public DelegateCommand SelectBgCommand { get; }
        private static void CloseLauncherAction(object pram)
        {
            SettingModel.Save();
            Application.Current.Shutdown();
        }


        public bool LaunchGameModalShow { get; set; }
        public string LaunchState { get; private set; }
        public double LaunchProgress { get; private set; }
        public void LaunchGameAction()
        {
            if (LauncherBasic.SelectedGame == null)
                return;
            
            IEnumerable<DependenceInspector.GameDependence> lost = DependenceInspector.Inspect(LauncherBasic.SelectedGame);
            IEnumerable<DependenceInspector.GameDependence> gameDependencies = lost as DependenceInspector.GameDependence[] ?? lost.ToArray();
            if (!gameDependencies.Any())
                LauncherBasic.LaunchGame();
            else
            {
                LaunchGameModalShow = true;
                FileDownloader[] downloads =
                    (from item in gameDependencies select new FileDownloader(item.DownloadUrl, item.FilePath)).ToArray();

                FileDownloaderWrap wrap = new FileDownloaderWrap(downloads);
                wrap.OnProgressChanged += (sender) =>
                {
                    LaunchState = $"正在补全文件:{sender.Progress:0.00}% ... {sender.Speed:0.0}kB/s";
                    LaunchProgress = sender.Progress;
                };
                wrap.OnCompleted += (sender) =>
                {
                    LaunchGameModalShow = false; 
                    LauncherBasic.LaunchGame();
                };
                wrap.BeginDownload();
            }
        }

        public LocalGameInfo SelectedGameInfo
        {
            get => LauncherBasic.SelectedGame;
            set
            {
                OnPropertyChanged();
                LauncherBasic.SelectedGame = value;

            }
        }
        public IEnumerable<string> JavaPaths => LauncherBasic.JavaPaths;

        public string PlayerName
        {
            get => LauncherBasic.PlayerName;
            set
            {
                LauncherBasic.PlayerName = value;
                OnPropertyChanged();
            }
        }
    }
}
